package com.xiongjie.health;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpHeaders;
import io.vertx.ext.healthchecks.HealthChecks;
import io.vertx.ext.healthchecks.Status;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;

public class EventBusConsumerHealthChecks extends AbstractVerticle {

    @Override
    public void start() {
        HealthChecks healthChecks = HealthChecks.create(vertx);
        healthChecks.register("my-procedure", future -> {
            future.complete(Status.OK());
            future.complete(Status.KO());
        });
        vertx.eventBus().<String>consumer("hello", message -> {
            healthChecks.invoke(message::reply);
        });

        Router router = Router.router(vertx);
        router.get("/").handler(this::hello);
        vertx.createHttpServer()
                .requestHandler(router)
                .listen(8080);
    }

    //这是路由处理器，决定http请求来后怎么返回页面内容
    private void hello(RoutingContext rc) {
        vertx.eventBus().request("hello", "ping", response -> {
            rc.response()
                    .putHeader(HttpHeaders.CONTENT_TYPE, "application/json")
                    .end(response.result().body().toString());
        });
    }
}
